package org.example.examsystem.service;


import org.example.examsystem.entity.Student ;
import org.example.examsystem.mapper.StudentMapper;
import org.example.examsystem.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Page<Student> getStudents(Pageable pageable, Integer classid) {
        int offset = pageable.getPageNumber() * pageable.getPageSize();
        int pageSize = pageable.getPageSize();
        List<Student> students = studentMapper.findAllStudents(offset, pageSize, classid);
        int total = studentMapper.countAllStudents(classid);
        return new PageImpl<>(students, pageable, total);
    }

    @Override
    public void addStudent(Student student) {
        studentMapper.insertStudent(student);
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Override
    public void deleteStudent(int id) {
        studentMapper.deleteStudent(id);
    }

    @Override
    public void deleteStudents(List<Integer> ids) {
        studentMapper.deleteStudents(ids);
    }
}